{include file="common/head"/}
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>工单列表</legend>
    </fieldset>
    <div class="demoTable">
        <button class="layui-btn" data-type="reload" onclick="location.reload()">刷新</button>
        <div class="layui-inline">
            <input class="layui-input" name="key" id="key" placeholder="{:lang('pleaseEnter')}编号或关键字">
        </div>
        <button class="layui-btn" id="search" data-type="reload">{:lang('search')}</button>
        <!--<a href="{:url('queryPage')}" class="layui-btn">显示全部</a>-->
        <button type="button" class="layui-btn layui-btn-danger" id="delAll">批量关闭</button>
        <button type="button" class="layui-btn" id="add">创建工单</button>
    </div>

    <table class="layui-table" id="list" lay-filter="list"></table>
</div>
{include file="common/foot"/}
<script type="text/html" id="option">

    {{# if(d.state_id==10){ }}

        <a class="layui-btn layui-btn-xs" lay-event="verify" >通过审核</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">关闭</a>

    {{# }else if(d.state_id==11 && gid==5){  }}

        <a class="layui-btn layui-btn-xs" lay-event="dis">创建派单</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">关闭</a>

    {{# }else if(d.state_id==12){  }}

    {{# }else{  }}

    {{# } }}
</script>

<script>
    var gid = '{:session("gid")}';
    layui.use('table', function() {
        var table = layui.table, $ = layui.jquery;
        var tableIn = table.render({
            id: 'works',
            elem: '#list',
            url: '{:url("queryPage")}',
            method: 'post',
            where:{state_id:'10,11,12'},
            page: true,
            cols: [[
                {checkbox: true, fixed: true},
                {field: 'number', title: '{:lang("id")}', width: 180,fixed: true},
                {field: 'title', title: '标题', width: 180},
                {field: 'tel', title: '{:lang("tel")}', width: 180},
                {field: 'name', title: '联系人', width: 120},
                {field: 'desc', title: '描述', width: 180},
                {field: 'address', title: '地址', width: 150},
                {field: 'type_name', title: '类型', width: 120},
                {field: 'fault_name', title: '故障类型', width: 150},
                {field: 'state_name', title: '状态', width: 80},
                {width: 180, align: 'center', toolbar: '#option'}
            ]],
            limit: 10 //每页默认显示的数量
        });
        //搜索
        $('#search').on('click', function () {
            var key = $('#key').val();
            if ($.trim(key) === '') {
                layer.msg('{:lang("pleaseEnter")}关键字！', {icon: 0});
                return;
            }
            tableIn.reload({
                where: {key: key,state_id:'10,11,12',page:1}
            });
        });
        table.on('tool(list)', function(obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('您确定要关闭工单吗？', function(index){
                    var loading = layer.load(1, {shade: [0.1, '#fff']});
                    $.post("{:url('close')}",{ids:data.id},function(res){
                        layer.close(loading);
                        if(res.code===1){
                            layer.msg(res.msg,{time:1000,icon:1});
                            tableIn.reload();
                        }else{
                            layer.msg('操作失败！',{time:1000,icon:2});
                        }
                    });
                    layer.close(index);
                });
            } else if(obj.event == 'verify'){
                $.post('{:url("Person/query")}',{gid:5},function (udata) {

                    var str = '<div id="eng" style="line-height: 45px;font-size: 17px;"><span>工程师：</span>' +
                            '<select name="aid" id="aid" style="padding: 5px 15px;">';

                        $.each(udata.data,function (k,v) {
                            str += '<option value="'+v.admin_id+'">'+v.username+'</option>';
                        });

                    str +='</select>'+
                        '</div>';
                    layer.confirm('您确定要审核工单吗？'+str, function(index){

                        var aid = $('#aid').val();

                        if(aid.length<=0) {
                            layer.msg('请选择工程师');
                            return;
                        }

                        var loading = layer.load(1, {shade: [0.1, '#fff']});
                        $.post("{:url('audit')}",{id:data.id,aid:aid},function(res){
                            layer.close(loading);
                            if(res.code===1){
                                layer.msg(res.msg,{time:1000,icon:1});
                                tableIn.reload();
                            }else{
                                layer.msg(res.msg,{time:1000,icon:2});
                            }
                        });
                        layer.close(index);
                    });
                });
            }else if(obj.event == 'dis'){

                location.href = '{:url("Dispatch/addUI")}?id='+data.id;

            }
        });
        $('#delAll').click(function(){
            layer.confirm('确认要关闭选中的工单吗？', {icon: 3}, function(index) {
                layer.close(index);
                var checkStatus = table.checkStatus('works'); //test即为参数id设定的值
                var ids = '';
                $(checkStatus.data).each(function (i, o) {
                    ids += o.id+',';
                });

                var loading = layer.load(1, {shade: [0.1, '#fff']});
                $.post("{:url('close')}", {ids: ids}, function (data) {
                    layer.close(loading);
                    if (data.code === 1) {
                        layer.msg(data.msg, {time: 1000, icon: 1});
                        tableIn.reload();
                    } else {
                        layer.msg(data.msg, {time: 1000, icon: 2});
                    }
                });
            });
        });

        $('#add').click(function () {
            location.href = '{:url("addUI")}';
        });
    });
</script>